package yijy.company;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by yijy on 2017/8/21.
 * 查询字符串中第一个不重复的字符
 */
public class FirstNonRepeatedChar {

    public static void main(String[] args) {
        String s = "sfjlfklsfjklsdfjkksdiaof";
        getFirstNonRepeatedChar(s);
    }

    public static String getFirstNonRepeatedChar(String s){
        if(null==s || "".equals(s)){
            return null;
        }
        String[] str = s.split("");
        Map<String,Integer> map = new HashMap<String,Integer>();
        for(int i =0;i<str.length;i++){
            if(map.containsKey(str[i])){
                map.put(str[i],map.get(str[i])+1);
            }else{
                map.put(str[i],1);
            }
        }

        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String,Integer>  en = ( Map.Entry<String,Integer>) iterator.next();
            if(en.getValue()==1){
                System.out.println(en.getKey());
                break;
            }
        }
        return null;
    }
}
